﻿var Profile =
{
    init : function()
    {
    
        $("#txtOldPassword").keydown(function(e){Profile._keyDownPassword(e);});
        $("#txtNewPassword").keydown(function(e){Profile._keyDownPassword(e);});
        $("#txtName").keydown(function(e){Profile._keyDownGeneralInfo(e);});
        $("#txtUrl").keydown(function(e){Profile._keyDownGeneralInfo(e);});
        $("#txtEmail").keydown(function(e){Profile._keyDownGeneralInfo(e);});
        
        $("#txtTwitterName").keydown(function(e){Profile._keyDownTwitter(e);});
        $("#txtTwitterPassword").keydown(function(e){Profile._keyDownTwitter(e);});
        
        
        $("#txtEmail").focus();
    },
    
    changePassword : function()
    {
    
        //hide validators and messages
        $("div.validator").hide();
        $("div.message").hide();
    
        var oldpassword = $("#txtOldPassword").val();
        var newpassword = $("#txtNewPassword").val();
        var message = $("#changePasswordMessage");
        var btnChangePassword = $("#btnChangePassword");
        
        if (oldpassword.length == 0)
        {
            Profile._showMessage('#valOldPassword', 'Password cannot be blank.', true);
            $("#txtOldPassword").focus();
            return;
        }
        
        if (newpassword.length == 0)
        {
            Profile._showMessage('#valNewPassword', 'Password cannot be blank.', true);
            $("#txtNewPassword").focus();
            return;
        }
        if (newpassword.length < 4)
        {
            Membership._showMessage('#valNewPassword', 'Password must be 4 character long.', true);
            $("#txtNewPassword").focus();
            return;
        }
        
        message.removeClass("hilite")
        Profile._showMessage('#changePasswordMessage', 'Changing password...', false);
        btnChangePassword.attr("disabled","disabled");
        
        $.ajax({ 
                type: "POST",
                dataType: "json",
                url: "/profile/changepassword",
                data: {oldpassword: oldpassword, newpassword: newpassword},
                success:function(result)
                {
                    btnChangePassword.removeAttr("disabled");

                    if (result.isSuccessful == true)
                    {
                        message.hide();
                        message.addClass("hilite").fadeIn(1000);
                        Profile._showMessage('#changePasswordMessage', "Password changed successfully", false);                        
                    }
                    else
                    {
                        var msg = result.errorMessage;
                        message.hide();
                        message.addClass("hilite").fadeIn(1000);
                        Profile._showMessage('#changePasswordMessage', msg, true);
                        $("#txtOldPassword").focus();
                    }
                },
                error:function(error)
                {
                    btnChangePassword.removeAttr("disabled");
                    Profile._showMessage('#changePasswordMessage', 'An unexpected error has occurred while changing password.', true);
                }
        });
        
    },
    
    updateTwitter : function()
    {
        //hide validators and messages
        $("div.validator").hide();
        $("div.message").hide();
        var username = $("#txtTwitterName").val();
        var password = $("#txtTwitterPassword").val();
        var btnSave = $("#btnTwitterSave");
        var message = $("#twitterMessage");
        
        if (username.length >0)
        {
            if (password.length == 0)
            {
                Membership._showMessage('#valTwitterPassword', 'Password cannot be blank.', true);
                $("#txtTwitterPassword").focus();
                return;
            }
        }
        
        message.removeClass("hilite")
        Profile._showMessage('#twitterMessage', 'Saving changes...', false);
        btnSave.attr("disabled","disabled");
        
        $.ajax({ 
                type: "POST",
                dataType: "json",
                url: "/profile/updatetwitter",
                data: {username: username,
                        password: password},
                success:function(result)
                {
                    btnSave.removeAttr("disabled");

                    if (result.isSuccessful == true)
                    {
                        message.hide();
                        message.addClass("hilite").fadeIn(1000);
                        Profile._showMessage('#twitterMessage', "Changes saved successfully...", false);                        
                    }
                    else
                    {
                        var msg = result.errorMessage;
                        message.hide();
                        message.addClass("hilite").fadeIn(1000);
                        Profile._showMessage('#twitterMessage', msg, true);
                    }
                },
                error:function(error)
                {
                    btnSave.removeAttr("disabled");
                    message.hide();
                    message.addClass("hilite").fadeIn(1000);
                    Profile._showMessage('#twitterMessage', 'An unexpected error has occurred while saving changes.', true);
                }
        });
    
    },
    
    updateProfile : function()
    {
        //hide validators and messages
        $("div.validator").hide();
        $("div.message").hide();
        
        var email = $.trim($("#txtEmail").val());
        var url = $("#txtUrl").val();
        var name = $("#txtName").val();
        var about = $("#txtAbout").val();
        var emailOnFollow = $("#chkEmailOnFollow").is(":checked");
        var emailOnResponse = $("#chkEmailOnReply").is(":checked");
        
        var message = $("#infoMessage");
        var btnSave = $("#btnSave");
        
        if (email.length == 0)
        {
            Membership._showMessage('#valEmail', 'Email cannot be blank.', true);
            $("#txtEmail").focus();
            return;
        }

        if (!Membership._isValidEmail(email))
        {
            Membership._showMessage('#valEmail', 'Invalid email address.', true);
            $("#txtEmail").focus();
            return;
        }
        
        var urlExp = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
        if (url.length > 0 && !urlExp.test(url))
        {
            Membership._showMessage('#valUrl', 'Url is in invalid.', true);
            $("#txtUrl").focus();
            return;
        }
        
        
        message.removeClass("hilite")
        Profile._showMessage('#infoMessage', 'Saving changes...', false);
        btnSave.attr("disabled","disabled");
        
        $.ajax({ 
                type: "POST",
                dataType: "json",
                url: "/profile/update",
                data: {email: email,
                        url: url,
                        about: about,
                        name: name,
                        emailOnResponse: emailOnResponse,
                        emailOnFollow: emailOnFollow},
                success:function(result)
                {
                    btnSave.removeAttr("disabled");

                    if (result.isSuccessful == true)
                    {
                        message.hide();
                        message.addClass("hilite").fadeIn(1000);
                        Profile._showMessage('#infoMessage', "Changes saved successfully...", false);                        
                    }
                    else
                    {
                        var msg = result.errorMessage;
                        message.hide();
                        message.addClass("hilite").fadeIn(1000);
                        Profile._showMessage('#infoMessage', msg, true);
                    }
                },
                error:function(error)
                {
                    btnSave.removeAttr("disabled");
                    message.hide();
                    message.addClass("hilite").fadeIn(1000);
                    Profile._showMessage('#infoMessage', 'An unexpected error has occurred while saving changes.', true);
                }
        });
        
    },
    
    _showMessage : function(e, msg, err)
    {
        if (err)
        {
            $(e).css("color" ,'#ff0000');
        }
        else
        {
            $(e).css("color", '');
        }
        $(e).text(msg);
        $(e).show();
    },
    
    _keyDownPassword : function(e)
    {
        if (e.keyCode === keys.enter)
        {
            Profile.changePassword();
        }
    },
    
    _keyDownGeneralInfo : function(e)
    {
        if (e.keyCode === keys.enter)
        {
            Profile.updateProfile();
        }
    },
    
    _keyDownTwitter : function(e)
    {
        if (e.keyCode === keys.enter)
        {
            Profile.updateTwitter();
        }        
    }

}